<?php
class Util
{

	static public function addSlashes($string) 
	{   
		if (is_array($string)) 
		{   
			foreach ($string as $key => $value) 
			{   
				$string[self::addSlashes($key)] = self::addSlashes($value);   
			}   
		} 
		else 
		{   
			$string = addslashes($string);   
		}   
		return $string;   
	}
	
	static public function import($string)
	{
		$reqireFile = '';
		$reStr = strtolower($string);
		if (stristr($reStr, 'model'))
		{
			$reqireFile = INC_PATH.DS.'model'.DS.$string.'.php';
		}
		elseif (stristr($reStr, 'action'))
		{
			$reqireFile = INC_PATH.DS.'action'.DS.$string.'.php';
		}
		
		if (file_exists($reqireFile))
		{
			require_once $reqireFile;
		}
		else
		{
			die('Don\'t find file: ' .$reqireFile);
		}
	}
	
	static public function M($classname, $funcname = 'index', $params = '')
	{
		$fileName = $classname.'Model';
		$reqireFile = INC_PATH.DS.'model'.DS.$fileName.'.php';
		if (file_exists($reqireFile))
		{
			require_once $reqireFile;
			return $fileName::$funcname($params);
		}
		else
		{
			die('Don\'t find model file: ' .$reqireFile);
		}
	}
	
	static public function A($classname, $funcname = 'index', $params = '')
	{
		$fileName = $classname.'Controller';
		$funcName = strtolower($funcname).'Action';
		
		Tpl::assign('ActionName', strtolower($funcname));
		$subModuleFile = Tpl::skinPath().'/mod_'.$classname.'_'.strtolower($funcname).'.php';
		Tpl::assign('SubModuleFile', $subModuleFile);
		$reqireFile = INC_PATH.DS.'controller'.DS.$fileName.'.php';
		if (file_exists($reqireFile))
		{
			require_once $reqireFile;
			if (method_exists($fileName, $funcName))
			{
				return $fileName::$funcName($params);
			}
			
		}
		else
		{
			//die('Don\'t find action file: ' .$reqireFile);
		}
	}
	
	static public function curPageURL() 
	{
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") 
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") 
		{
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} 
		else 
		{
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
	
	static public function curRootURL()
	{
		$curUrl = self::curPageURL();
		$result = substr($curUrl, 0, strrpos($curUrl, "/"));
		return $result;
	}
	
	static public function redirect($url)
	{
		header('Location:'.$url);
		exit;
	}
	
}